package com.hbu.product.hbumallproduct.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.Query;
import com.hbu.product.hbumallproduct.dao.AttrAttrgroupRelationDao;
import com.hbu.product.hbumallproduct.dao.AttrDao;
import com.hbu.product.hbumallproduct.dao.AttrGroupDao;
import com.hbu.product.hbumallproduct.entity.AttrAttrgroupRelationEntity;
import com.hbu.product.hbumallproduct.entity.AttrGroupEntity;
import com.hbu.product.hbumallproduct.service.AttrAttrgroupRelationService;
import com.hbu.product.hbumallproduct.vo.AddAttrRelationVO;
import com.hbu.product.hbumallproduct.vo.AttrAttrGroupListChildrenVO;
import com.hbu.product.hbumallproduct.vo.AttrAttrGroupListVO;
import com.hbu.product.hbumallproduct.vo.AttrRelationDeleteVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @author 90458
 */
@Service("attrAttrgroupRelationService")
@AllArgsConstructor
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    private AttrGroupDao attrGroupDao;

    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void relationDelete(AttrRelationDeleteVO[] attrRelationDeleteVOList) {
        this.baseMapper.relationDelete(attrRelationDeleteVOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAttrRelation(List<AddAttrRelationVO> addAttrRelationVOList) {
        saveBatch(addAttrRelationVOList.stream().map(addAttrRelationVO -> {
            Optional<AttrGroupEntity> attrGroupEntityOptional = Optional.ofNullable(attrGroupDao.selectById(addAttrRelationVO.getAttrGroupId()));
            AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelation.setAttrId(addAttrRelationVO.getAttrId());
            attrAttrgroupRelation.setAttrGroupId(addAttrRelationVO.getAttrGroupId());
            attrGroupEntityOptional.ifPresent(attrGroupEntity -> {
                attrAttrgroupRelation.setCatelogId(attrGroupEntity.getCatelogId());
                attrAttrgroupRelation.setAttrSort(attrGroupEntity.getSort());
            });
            return attrAttrgroupRelation;
        }).collect(Collectors.toList()));

    }

    @Override
    public List<AttrAttrGroupListVO> getAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntityList = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        return attrGroupEntityList.stream().map(attrGroup -> {
            AttrAttrGroupListVO attrAttrGroupListVO = new AttrAttrGroupListVO(attrGroup);
            List<Long> attrIdList = baseMapper.getAttrIdByAttrGroupId(attrAttrGroupListVO.getAttrGroupId());
            attrAttrGroupListVO.setAttrs(attrDao.selectBatchIds(attrIdList).stream().map(attrEntity -> {
                AttrAttrGroupListChildrenVO attrAttrGroupListChildrenVO = new AttrAttrGroupListChildrenVO();
                BeanUtil.copyProperties(attrEntity, attrAttrGroupListChildrenVO);
                attrAttrGroupListChildrenVO.setAttrGroupId(attrGroup.getAttrGroupId());
                return attrAttrGroupListChildrenVO;
            }).collect(Collectors.toList()));
            return attrAttrGroupListVO;
        }).collect(Collectors.toList());
    }


}